#!/usr/bin/env bash
source ./config/env.sh
clear


mkdir -p ${K8S_DATA}/package

# k8s cline package download
while true
do
sleep 2s
test -e ${K8S_DATA}/package/kubernetes-client-linux-amd64.tar.gz
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/kubernetes-client-linux-amd64.tar.gz
else
    client="`sha256sum ${K8S_DATA}/package/kubernetes-client-linux-amd64.tar.gz | cut -d " " -f 1`"
    if [[ $client == "4b98423650e4b12f3867453041e65d617a7d1511943837b3359c11accca3d862" ]];then
        break
    else
        rm -fr ${K8S_DATA}/package/kubernetes-client-linux-amd64.tar.gz
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/kubernetes-client-linux-amd64.tar.gz
    fi
fi
done

# k8s server package download
while true
do
sleep 2s
test -e ${K8S_DATA}/package/kubernetes-server-linux-amd64.tar.gz
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/kubernetes-server-linux-amd64.tar.gz
else
    server="`sha256sum ${K8S_DATA}/package/kubernetes-server-linux-amd64.tar.gz | cut -d " " -f 1`"
    if [[ $server == "39e9fb6f6293982f117a29f13ee37bfb5b4cb147ae06a13956b603e3b196b013" ]];then
        break
    else
        rm -fr ${K8S_DATA}/package/kubernetes-server-linux-amd64.tar.gz
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/kubernetes-server-linux-amd64.tar.gz
    fi
fi
done

# k8s etcd package download
while true
do
sleep 2s
test -e ${K8S_DATA}/package/etcd-v3.3.12-linux-amd64.tar.gz
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/etcd-v3.3.12-linux-amd64.tar.gz
else
    etcd="`sha256sum ${K8S_DATA}/package/etcd-v3.3.12-linux-amd64.tar.gz | cut -d " " -f 1`"
    if [[ $etcd == "dc5d82df095dae0a2970e4d870b6929590689dd707ae3d33e7b86da0f7f211b6" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/etcd-v3.3.12-linux-amd64.tar.gz
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/etcd-v3.3.12-linux-amd64.tar.gz
    fi
fi
done

# k8s flannel package download
while true
do
sleep 2s
test -e ${K8S_DATA}/package/flannel-v0.11.0-linux-amd64.tar.gz
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/flannel-v0.11.0-linux-amd64.tar.gz
else
    flannel="`sha256sum ${K8S_DATA}/package/flannel-v0.11.0-linux-amd64.tar.gz | cut -d " " -f 1`"
    if [[ $flannel == "136e859d5e5239d12effe312d599c3978f94d1b6ecff6bd18695cf022030f93a" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/etcd-v3.3.12-linux-amd64.tar.gz
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/flannel-v0.11.0-linux-amd64.tar.gz
    fi
fi
done



# Docker package download
while true
do
sleep 2s
test -e ${K8S_DATA}/package/docker-18.09.3.tgz
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/docker-18.09.3.tgz
else
    docker="`sha256sum ${K8S_DATA}/package/docker-18.09.3.tgz | cut -d " " -f 1`"
    if [[ $docker == "8b886106cfc362f1043debfe178c35b6f73ec42380b034a3919a235fe331e053" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/docker-18.09.3.tgz
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/docker-18.09.3.tgz
    fi
fi
done

# Docker-compose package download
while true
do
sleep 2s
test -e ${K8S_DATA}/package/docker-compose-Linux-x86_64
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/docker-compose-Linux-x86_64
else
    dockercompose="`sha256sum ${K8S_DATA}/package/docker-compose-Linux-x86_64 | cut -d " " -f 1`"
    if [[ $dockercompose == "4d618e19b91b9a49f36d041446d96a1a0a067c676330a4f25aca6bbd000de7a9" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/docker-18.09.3.tgz
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/docker-compose-Linux-x86_64
    fi
fi
done

# Generate the official package
while true
do
sleep 2s
test -e ${K8S_DATA}/package/cfssl-certinfo_linux-amd64
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/cfssl-certinfo_linux-amd64
else
    cfsslinfo="`sha256sum ${K8S_DATA}/package/cfssl-certinfo_linux-amd64 | cut -d " " -f 1`"
    if [[ $cfsslinfo == "ad395c35824bdff343189188658f15810d137c230463810026c6f04f3f78f08f" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/cfssl-certinfo_linux-amd64
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/cfssl-certinfo_linux-amd64
    fi
fi
done


while true
do
sleep 2s
test -e ${K8S_DATA}/package/cfssljson_linux-amd64
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/cfssljson_linux-amd64
else
    cfsslinfo="`sha256sum ${K8S_DATA}/package/cfssljson_linux-amd64 | cut -d " " -f 1`"
    if [[ $cfsslinfo == "1c9e628c3b86c3f2f8af56415d474c9ed4c8f9246630bd21c3418dbe5bf6401e" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/cfssljson_linux-amd64
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/cfssljson_linux-amd64
    fi
fi
done


while true
do
sleep 2s
test -e ${K8S_DATA}/package/cfssl_linux-amd64
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/cfssl_linux-amd64
else
    cfsslinfo="`sha256sum ${K8S_DATA}/package/cfssl_linux-amd64 | cut -d " " -f 1`"
    if [[ $cfsslinfo == "eb34ab2179e0b67c29fd55f52422a94fe751527b06a403a79325fed7cf0145bd" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/cfssl_linux-amd64
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/cfssl_linux-amd64
    fi
fi
done

while true
do
sleep 2s
test -e ${K8S_DATA}/package/heapster-1.5.4.tar.gz
if [[ $? != 0 ]];then
    wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/heapster-1.5.4.tar.gz
else
    cfsslinfo="`sha256sum ${K8S_DATA}/package/heapster-1.5.4.tar.gz | cut -d " " -f 1`"
    if [[ $cfsslinfo == "2bcd7a2c53b778e89d84d80107f60d5fcf920585b7c5d94b691383db3b2af264" ]];then
        break
    else
        rm -rf ${K8S_DATA}/package/heapster-1.5.4.tar.gz
        wget -P ${K8S_DATA}/package http://123.207.151.170:7011/v1.13.4/package/heapster-1.5.4.tar.gz
    fi
fi
done